//
//  ModifyFriendNameViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/15.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "ModifyFriendNameViewController.h"

@interface ModifyFriendNameViewController ()
/**
 title
 */
@property (nonatomic , strong) UILabel *titleLab;
/**
 view
 */
@property (nonatomic , strong) UIView *bgView;
/**
 text
 */
@property (nonatomic , strong) UITextField *textFiled;
@end

@implementation ModifyFriendNameViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"备注豚友名称";
    [self setUpNav];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpNav {
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 26)];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancelButton setTitleColor:[UIColor colorWithHexString:@"#656565"] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
    self.navigationItem.leftBarButtonItem = cancelItem;
   
    UIButton *saveButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 56, 26)];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    saveButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    saveButton.backgroundColor = [UIColor primaryColor];
    saveButton.layer.cornerRadius = 5;
    saveButton.layer.masksToBounds = YES;
    [saveButton addTarget:self action:@selector(saveButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithCustomView:saveButton];
    self.navigationItem.rightBarButtonItem = saveItem;
}
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.titleLab];
    [self.view addSubview:self.bgView];
    [self.bgView addSubview:self.textFiled];
}

- (void) initLayout {
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(16);
        make.height.mas_equalTo(18);
    }];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(5);
        make.height.mas_equalTo(56);
    }];
    [self.textFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.bgView.mas_centerY);
        make.height.mas_equalTo(56);
    }];
}

- (void) initAction {
    
}

- (void)initData {
    
}

#pragma mark - delegate

#pragma mark - target action
- (void)cancelButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)saveButtonClick {
    NSString *modifyName = self.textFiled.text;

    if ([JTool iSNull:modifyName]) {
        [self toastWithContent:@"请输入备注名称"];
        return;
    }
    @weakify(self);
    [[TIMFriendshipManager sharedInstance] modifyFriend:self.identifier values:@{TIMFriendTypeKey_Remark:modifyName} succ:^{
        @strongify(self);
        [self modifFriendNameRequestWithName:modifyName];
    } fail:^(int code, NSString *msg) {
        @strongify(self);
        [self toastWithContent:msg];
    }];
}
#pragma mark - private method
- (void)modifFriendNameRequestWithName:(NSString *)name {
   
    @weakify(self);
    [[[PigMarketApi sharedInstance] modifyFriendNameWithMemberId:[UserInfoDataProvider sharedInstance].userInfo.memberId memberNum:self.identifier noteName:name] subscribeNext:^(NSNull * _Nullable x) {
        @strongify(self);
        [self toastWithContent:@"修改成功"];
        if (self.refreshChatSetUpDataBock) {
            self.refreshChatSetUpDataBock(name);
        }
        
        //通知我的好友列表刷新数据
        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationFriendRemarkChange object:self];

         @weakify(self);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            @strongify(self);
//            [self.rtNavigationController removeViewControllerWithName:@"ChatSetUpViewController"];
//            [self.rtNavigationController removeViewControllerWithName:@"ChatViewController"];
            [self.navigationController popViewControllerAnimated:YES];
        });
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
    }];
}
#pragma mark - getter and setter
- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor colorWithHexString:@"#B0B0B0"];
        _titleLab.font = [UIFont systemFontOfSize:13];
        _titleLab.text = @"豚友备注";
    }
    return _titleLab;
}
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}
- (UITextField *)textFiled {
    if (!_textFiled) {
        _textFiled = [[UITextField alloc] init];
        _textFiled.placeholder = @"请输入备注名称";
        _textFiled.textColor = [UIColor colorWithHexString:@"#B0B0B0"];
        _textFiled.font = [UIFont systemFontOfSize:16];

        [_textFiled setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.textColor"];
        [_textFiled setValue:[UIColor colorWithHexString:@"#B0B0B0"] forKeyPath:@"_placeholderLabel.textColor"];
    }
    return _textFiled;
}
@end
